<template>
  <h2>当前求和为：{{ sum }}</h2>
  <button @click="changeSum">点我加一</button>
  <hr />
  <h2>当前msg：{{ msg }}</h2>
  <button @click="msg += '!'">修改信息</button>
  <hr />
  <h2>姓名：{{ person.name }}</h2>
  <h2>年龄：{{ person.age }}</h2>
  <h2>薪水：{{ person.job.j1.salary }}</h2>
  <button @click="person.name += '!'">修改姓名</button>
  <button @click="person.age++">修改年龄</button>
  <button @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
import { reactive, ref, watch } from "vue";
export default {
  name: "Demo",
  //   watch: {
  //     //  简单写法
  //     //  sum(newValue, oldValue) {
  //     //   console.log(newValue, oldValue);
  //     // },
  //     // 完整写法
  //     sum: {
  //       // 立即执行一次
  //       immediate: true,
  //       //  深度监视
  //       deep: true,
  //       handler(newValue, oldValue) {
  //         console.log(newValue, oldValue);
  //       },
  //     },
  //   },
  setup() {
    let sum = ref(0);
    let msg = ref("你好");
    let person = ref({
      name: "张三",
      age: 99,
      job: {
        j1: {
          salary: 18,
        },
      },
    });
    // 监视的不应该是数字本身，而应该是保存这个数据的结构
    watch(sum, (newValue, oldValue) => {
      console.log("sum值变化了", newValue, oldValue);
    });
    watch(person.value, (newValue, oldValue) => {
      console.log("person变化了", newValue, oldValue);
    });
    watch(
      person,
      (newValue, oldValue) => {
        console.log("person变化了", newValue, oldValue);
      },
      { deep: true }
    );

    function changeSum() {
      sum.value++;
    }
    return {
      sum,
      msg,
      person,
      changeSum,
    };
  },
};
</script>

<style>
</style>
